package comchifaner.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 钉钉
 * Title: RestClient<br>
 * Description: <br>
 * Copyright @ 2012~2016 chifaner.com .All rights reserved.<br>
 * @author tao.zhang
 * @createDate 2016年4月5日
 * @version v1.0
 */
public class DingClient {

	private static String systemFlag="团餐系统：";
	public static boolean send(String urlStr, String mobile, String content){
		boolean flag=false;
		//需要请求的restful地址
		String output=null;
	    
		URL url;
		try {
			url = new URL(urlStr);
		//打开restful链接
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		          
		// 提交模式
		conn.setRequestMethod("POST");//POST GET PUT DELETE
		          
		//设置访问提交模式，表单提交
		conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		          
		conn.setConnectTimeout(10000);//连接超时 单位毫秒
		conn.setReadTimeout(2000);//读取超时 单位毫秒
		          
		conn.setDoOutput(true);// 是否输入参数

		// 表单参数与get形式一样
		StringBuffer params = new StringBuffer();
		params.append("mobile").append("=").append(mobile);
		params.append("&content").append("=").append(systemFlag+content);
		byte[] bypes = params.toString().getBytes();
		conn.getOutputStream().write(bypes);// 输入参数
		//读取请求返回值
		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
		           (conn.getInputStream())));
		
		System.out.println("Output from Server:  \n");
		while ((output = responseBuffer.readLine()) != null) {
		       System.out.println(output);
		       break;
		}
		conn.disconnect();
		
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}
		
		if(output!=null){
			flag=true;
		}
		return flag;
	}
	
	public static void main(String ...strings){
		//boolean flag=new DingClient("http://101.200.192.18:9879/message/text","18701003926","有乱码？").send();
		//System.out.println(flag);
	}

}
